<template>
  <div>
    <div class="panel panel-default">
      <div class="panel-heading">
        <el-button type="text" icon="el-icon-refresh" @click="loadModules"></el-button>
      </div>
      <div class="panel-body">
        <div style="display: flex;align-items: center">
          <b >Maven Settings: </b>
          <connect-chose module="maven" ref="mavenConnect" v-model="mavenSettings" />
        </div>

        <el-table
          :data="modules"
          size="mini"
          row-key="relativePath"
          default-expand-all
          height="600"
          v-loading="loading"
        >
          <el-table-column type="index" width="50"/>
          <el-table-column prop="moduleName" label="模块名"/>
          <el-table-column label="上次编译时间" width="150">
            <template slot-scope="{row}">
              {{row.lastCompileTime ? parseTime(row.lastCompileTime) : '未编译'}}
            </template>
          </el-table-column>

          <el-table-column label="上次获取jar包时间" width="150">
            <template slot-scope="{row}">
              {{row.classpathResolveTime ? parseTime(row.classpathResolveTime) : '未获取'}}
            </template>
          </el-table-column>

          <el-table-column
            fixed="right"
            label="操作"
            width="300"
          >
            <template slot-scope="{row}">
              <el-button type="text" size="small" @click="execMavenGoals(row.relativePath,['clean','compile'])">compile</el-button>
              <el-button type="text" size="small"  @click="execMavenGoals(row.relativePath,['clean','install'])">install</el-button>
              <el-button type="text" size="small"  @click="resolveJars(row.relativePath)">classpath</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>

    <!-- classpath 结果展示 -->
    <el-dialog :title="dialog.classpathResult.title" :visible.sync="dialog.classpathResult.visible">
      <resolvejars :tree-files="dialog.classpathResult.data" />
    </el-dialog>

    <!-- 查看执行日志 -->
    <el-dialog fullscreen :visible.sync="dialog.mavenGoalLogDialog.visible" append-to-body :title="dialog.mavenGoalLogDialog.title" @open="initMavenLog"  @close="cascadeMavenLog" >
      <maven-exec-goal-log :file-path="dialog.mavenGoalLogDialog.filePath" ref="mavenLog"/>
    </el-dialog>
  </div>
</template>

<script>
import vcs from '@/api/vcs'

import ConnectChose from '@/views/components/ConnectChose'
import resolvejars from '@/views/call/vcs/git/resolvejars'
import mavenExecGoalLog from '@/views/call/maven/mavenExecGoalLog'

import {parseTime} from '@/utils'

export default {
  name: 'projectModule',
  components: {ConnectChose, resolvejars,mavenExecGoalLog},
  props: {
    projectLocation: Object
  },
  data(){
    return {
      modules: [],
      loading: false,
      mavenSettings: null,
      dialog: {
        classpathResult: {
          visible: false,
          title: '获取 classpath 结果',
          data: []
        },
        mavenGoalLogDialog: {
          visible: false,
          title: '编译日志',
          filePath: null
        }
      }
    }
  },
  mounted() {
    this.loadModules()
  },
  methods: {
    cascadeMavenLog(){
      this.$nextTick(() => {
        this.$refs.mavenLog.stopInterval()
      })
    },
    initMavenLog(){
      this.$nextTick(() => {
        this.$refs.mavenLog.init()
      })
    },
    parseTime,
    loadModules(){
      if (!this.projectLocation){
        return
      }

      this.loading = true
      vcs.project.modules(this.projectLocation).then(res => {
        this.loading = false
        this.modules = res.data
      }).catch(() => {
        this.loading = false
      })
    },
    execMavenGoals(relativePath,goals){
      let settings = this.$refs.mavenConnect.getChoseConnect()
      let projectLocation = this.projectLocation;
      let mavenGoalsParam = {settingsName: settings,projectLocation:projectLocation,relativePomFile: relativePath,goals: goals}
      vcs.project.executeGoals(mavenGoalsParam).then(res => {
        // console.log(res.data)
        this.dialog.mavenGoalLogDialog.visible = true
        this.dialog.mavenGoalLogDialog.filePath = res.data.outFilePath
      })
    },
    resolveJars(relativePomFile){
      let settings = this.$refs.mavenConnect.getChoseConnect()
      let projectLocation = this.projectLocation;
      let moduleResolveDepParam = {settings,projectLocation,relativePomFile}
      this.loading = true
      vcs.project.resolveDependencies(moduleResolveDepParam).then(res => {
        // console.log(res.data,'resolveDependencies')
        this.loading = false
        // this.$message('获取 classpath 成功')

        this.dialog.classpathResult.data = [res.data]
        this.dialog.classpathResult.visible = true
      }).catch(() => {
        this.loading = false
      })
    }
  }
}
</script>

<style scoped>
  @import "../../../../assets/custom-theme/bootstrap.css";
</style>
